package cn.ranko.shiro.service;

import cn.ranko.bean.TreeData;
import cn.ranko.shiro.bean.authority.Menu;
import cn.ranko.shiro.bean.authority.MenuItem;
import cn.ranko.shiro.bean.authority.Module;
import cn.ranko.shiro.mapper.SysMenuMapper;
import cn.ranko.shiro.model.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhujun on 2017/8/13.
 */
@Service
public class SysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    /**
     * 初始化左侧菜单的数据，1.左侧菜单的显示，2.权限管理的配置设置
     * @return
     */
    public List<Menu> getMenus(){
        List<Menu> menus = new ArrayList<>();

        SysMenu parentSysMenu = new SysMenu();
        parentSysMenu.setParentId(0);
        parentSysMenu.setType("menu");
        List<SysMenu> sysMenus_menu = sysMenuMapper.selectBySysMenu(parentSysMenu);
        for (SysMenu menu: sysMenus_menu){
            Menu menuobject = new Menu();
            menuobject.setMenutext(menu.getMenutext());
            menuobject.setMenupermission(menu.getMenupermission());
            //查询menuitem
            SysMenu menuitemParent = new SysMenu();
            menuitemParent.setParentId(menu.getMenuId());
            menuitemParent.setType("menuitem");
            List<SysMenu> sysMenus_menuitem = sysMenuMapper.selectBySysMenu(menuitemParent);
            if(sysMenus_menuitem.size()>0){
                List<MenuItem> menuitemlist = new ArrayList<>();
                for(SysMenu menuitem :sysMenus_menuitem){
                    MenuItem item = new MenuItem();
                    item.setMenuitemtext(menuitem.getMenutext());
                    item.setMenuitempermission(menuitem.getMenupermission());
                    item.setMenuitemurl(menuitem.getMenuitemurl());
                    //查询module
                    SysMenu moduleParent = new SysMenu();
                    moduleParent.setParentId(menuitem.getMenuId());
                    moduleParent.setType("module");
                    List<SysMenu> sysmenu_modules = sysMenuMapper.selectBySysMenu(moduleParent);
                    if(sysmenu_modules.size()>0){
                        List<Module> modulelist = new ArrayList<>();
                        for (SysMenu module:sysmenu_modules){
                            Module moduleitem = new Module();
                            moduleitem.setModulename(module.getMenutext());
                            moduleitem.setModulepermission(module.getMenupermission());
                            modulelist.add(moduleitem);
                        }
                        item.setModules(modulelist);
                    }
                    menuitemlist.add(item);
                }
                menuobject.setMenuItems(menuitemlist);
            }
            menus.add(menuobject);
        }

        return menus;
    }

    public List<TreeData> loadTreeData() {
        List<TreeData> treeDatas = new ArrayList<>();
        SysMenu parentSysMenu = new SysMenu();
        parentSysMenu.setParentId(0);
        parentSysMenu.setType("menu");
        List<SysMenu> sysMenus_menu = sysMenuMapper.selectBySysMenu(parentSysMenu);
        for (SysMenu menu: sysMenus_menu){
            TreeData menuTreeData = new TreeData();
            menuTreeData.setId(menu.getMenuId());
            menuTreeData.setText(menu.getMenutext());
            menuTreeData.setAttributes(menu);

            SysMenu menuitemParent = new SysMenu();
            menuitemParent.setParentId(menu.getMenuId());
            menuitemParent.setType("menuitem");
            List<SysMenu> sysMenus_menuitem = sysMenuMapper.selectBySysMenu(menuitemParent);
            if(sysMenus_menuitem.size()>0){
                for(SysMenu menuitem :sysMenus_menuitem){
                    TreeData menuitemTreeData = new TreeData();
                    menuitemTreeData.setId(menuitem.getMenuId());
                    menuitemTreeData.setText(menuitem.getMenutext());
                    menuitemTreeData.setAttributes(menuitem);

                    SysMenu moduleParent = new SysMenu();
                    moduleParent.setParentId(menuitem.getMenuId());
                    moduleParent.setType("module");
                    List<SysMenu> sysMenus_module = sysMenuMapper.selectBySysMenu(moduleParent);
                    if(sysMenus_module.size()>0){
                        for (SysMenu module:sysMenus_module){
                            TreeData treeDataModule = new TreeData();
                            treeDataModule.setId(module.getMenuId());
                            treeDataModule.setText(module.getMenutext());
                            treeDataModule.setAttributes(module);
                            menuitemTreeData.getChildren().add(treeDataModule);
                        }
                    }
                    menuTreeData.getChildren().add(menuitemTreeData);
                }
            }
            treeDatas.add(menuTreeData);
        }
        return treeDatas;
    }
}
